package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.ShMerchantDao;
import com.shop.shoppingmall.entity.ShMerchant;
import com.shop.shoppingmall.entity.ShProduct;
import com.shop.shoppingmall.service.ShMerchantService;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (ShMerchant)表服务实现类
 *
 * @author makejava
 * @since 2020-09-09 14:53:50
 */
@Service("shMerchantService")
@Transactional
public class ShMerchantServiceImpl implements ShMerchantService {
    @Resource
    private ShMerchantDao shMerchantDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShMerchant queryById(String id) {
        return this.shMerchantDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ShMerchant> queryAllByLimit(int offset, int limit) {
        return this.shMerchantDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param shMerchant 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(ShMerchant shMerchant) {
        shMerchant.setId(UUIDGenerator.generate());
        shMerchantDao.insert(shMerchant);
        return Result.ok("添加成功!");
    }

    /**
     * 修改数据
     *
     * @param shMerchant 实例对象
     * @return 实例对象
     */
    @Override
    public Result update(ShMerchant shMerchant) {
        this.shMerchantDao.update(shMerchant);
       return Result.ok("修改成功!");
    }



    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.shMerchantDao.deleteById(id) > 0;
    }
    @Override
    public Result listAll(Map map){
        Integer pagenum = (Integer) map.get("pagenum");
        Integer pagesize = (Integer) map.get("pagesize");
        int a = (pagenum-1)*pagesize;
        System.out.println(a);
        map.put("offset",a);
        map.put("limit",pagesize);
        List<ShMerchant> shMerchantList = shMerchantDao.queryAll(map);
        if(shMerchantList == null){
            return Result.error("暂时还没有店铺信息！");
        }
        ShMerchant shMerchant = shMerchantDao.queryCount(map);
        Map map1 = new HashMap();
        map1.put("total", shMerchant.getTotal());
        map1.put("shMerchantList",shMerchantList);
        return Result.ok(map1);
    }

    @Override
    public Result selectUserId(String userId) {
        List<ShMerchant> shMerchants = shMerchantDao.selectByUserId(userId);
        if(shMerchants.size()>0){
            return Result.ok(shMerchants.get(0));
        }else{
            return Result.error("请添加店铺");
//            return Result.error("new ShMerchant()");
        }

    }


}
